package com.siriframe.security.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siriframe.security.dao.MenuMapper;
import com.siriframe.security.dao.UserMapper;
import com.siriframe.security.entity.po.LoginUser;
import com.siriframe.security.entity.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        //判断了
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<String> permissions = menuMapper.selectPermsByUserId(user.getId());
        //密码比对的那部分，SpringSecurity自动帮你做了
        return new LoginUser(user,permissions);//如何去校验用户名和密码的？是通过调用LoginUser中的getUsername和getPassword方法进行校验的
    }
}